﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;
using System.Net;
using System.IO;

namespace bsitProject
{
    public partial class C1 : Form
    {
        public C1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //Get the object used to communicate with the server
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://152.226.232.128/upload/RFQ.xml");
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            //This example assumes the FTP sites uses anonymous logon
            request.Credentials = new NetworkCredential("Sharon", "123456");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            
            File.WriteAllText("D:\\RFQ.xml", reader.ReadToEnd());

           // Console.WriteLine(reader.ReadToEnd());
            MessageBox.Show("Download Complete, status{0}", response.StatusDescription);

            reader.Close();
            response.Close();

        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            //dom, read xml

            dataGridView1.ClearSelection();

            //load XML file
            XmlDocument rfq = new XmlDocument();
            rfq.Load(@"D:\\RFQ.xml");
            
            //get details of quotation
            //string item = rfq.GetElementsByTagName("Item");

            XmlNodeList rfqList1 = rfq.GetElementsByTagName("RFQid");
            XmlNode x1 = rfqList1[0];
            string rfqID = x1.InnerText;

            //Search
            XmlNodeList rfqList = rfq.GetElementsByTagName("item");
            StringBuilder list = new StringBuilder();
            
            foreach (XmlNode node in rfqList)
            {
                if (node.Attributes.Count > 0)
                    list.Append("Product Item: ").Append(node.Attributes[0].Value).Append(Environment.NewLine);

                if (node.HasChildNodes)
                {
                    XmlElement childNode = (XmlElement)node.FirstChild;
                    for (int i = 0; i < node.ChildNodes.Count; i++)
                    {
                        switch (childNode.Name)
                        {
                            case "quantity":
                                list.Append("Quantity: ");
                                break;
                            case "askingPrice":
                                list.Append("Asking Price:");
                                break;
                            default:
                                break;
                        }
                        list.Append(childNode.InnerText).Append(Environment.NewLine);
                        childNode = (XmlElement)childNode.NextSibling;
                    }
                }
                list.Append(Environment.NewLine);
            }
            tbxOutput.Clear();
            tbxOutput.Text += list;

            /////////////////////////
            //INSERT INTO DATABASE//
            ///////////////////////

            // set up connection to the database and the sql query
            //SqlConnection conn = new SqlConnection
            //("server=TANUWIDJAJA;initial catalog=G:\\SEMESTER 3.1 TERM A\\BSIT\\PROJECT\\DATABASE\\SUPPLIER2DATABASE.MDF;integrated security=yes");

            SqlConnection conn = new SqlConnection
           ("server=localhost;initial catalog=D:\\SUPPLIER2DATABASE.MDF;integrated security=yes");
            string strSQL =
            "insert into RFQDetail (RFQID, ItemID, Quantity, AskingPrice) " +
            "values (@RFQID, @ItemID, @Quantity, @AskingPrice);";
            conn.Open();

            try
            {

                // process XML data using DOM approach
                XmlDocument rfq1 = new XmlDocument();
                rfq1.Load(@"C:\\Users\\Tanuwidjaja\\Documents\\Visual Studio 2010\\Projects\\bsitProject\\bsitProject\\RFQ.xml");
                XmlNodeList list1 = rfq1.GetElementsByTagName("item");
                foreach (XmlNode node in list1)
                {
                    // set the parameters of the sql insert statement
                    SqlCommand cmd = new SqlCommand(strSQL, conn);
                    cmd.Parameters.AddWithValue("@RFQID", x1.InnerText);
                    cmd.Parameters.AddWithValue("@ItemID", node.Attributes[0].Value);
                    cmd.Parameters.AddWithValue("@Quantity", node.ChildNodes[0].InnerText);
                    cmd.Parameters.AddWithValue("@AskingPrice", node.ChildNodes[1].InnerText);
                    cmd.ExecuteNonQuery();
                }
            }
            catch(Exception ex)
            {
                tbxOutput.Clear();
                MessageBox.Show("Error! Data has been inserted to database");
            }
            conn.Close();

            //////////////////////////
            //DATA GRID VIEW DETAILS//
            //////////////////////////

            // TODO: This line of code loads data into the 'supplier2DatabaseDataSet1.RFQDetail' table. You can move, or remove it, as needed.
            this.rFQDetailTableAdapter.Fill(this.supplier2DatabaseDataSet1.RFQDetail);
            
        }

        private void C1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'supplier2DatabaseDataSet1.RFQDetail' table. You can move, or remove it, as needed.
            //this.rFQDetailTableAdapter.Fill(this.supplier2DatabaseDataSet1.RFQDetail);
            
            // TODO: This line of code loads data into the 'supplier2DatabaseDataSet2.RFQDetail' table. You can move, or remove it, as needed.
            //this.rFQDetailTableAdapter.Fill(this.supplier2DatabaseDataSet2.RFQDetail);
        }

    }
}
